package com.nl.controller.web.service;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.interceptor.SysContext;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.common.util.AdcFunction;
import com.nl.controller.web.bean.ReportMonth;
import com.nl.controller.web.bean.SmsCountMonth;
import com.nl.controller.web.bean.SmsOperLog;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.mapper.SmsCountMonthMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SmsCountMonthService {
    @Autowired
    private SmsCountMonthMapper smsCountMonthMapper;

    @Autowired
    private CommonService commonService;


    @SystemServiceLog(description = "统计汇总数据获取", value = false)
    public List<SmsCountMonth> getDataList(String year) {
        HttpSession session = SysContext.getRequest().getSession();
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        List<SmsCountMonth> list = new ArrayList();
        //防止空指针
        SmsCountMonth tempCountMonth = new SmsCountMonth();
        tempCountMonth.setNormalNum("0");
        tempCountMonth.setLongNum("0");
        tempCountMonth.setLongNumSplit("0");
        tempCountMonth.setLongNumCount("0");
        tempCountMonth.setNormalNumCount("0");
        String duty = user.getDuty();
        Map<String, Object> ma = new HashMap<>();
        //数据筛选
        if ("1".equals(duty)) {
            //地市管理员
            ma.put("cityId", user.getCityId());

        } else if ("2".equals(duty)) {
            //项目管理员
            ma.put("cityId", user.getCityId());
            ma.put("mgrId", user.getMgrId().split(","));
        }
        ma.put("year", year);
        try {
            list = smsCountMonthMapper.accountCountMgr(ma);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取项目信息");
            throw new SmsException("获取项目信息出现异常");
        }
        for (int i = 0; i < list.size(); i++) {
            List<SmsCountMonth> dataList = new ArrayList<>();
            for (int j = 1; j < 13; j++) {
                Map map = new HashMap();
                SmsCountMonth countMonth;
                map.put("mgrId", list.get(i).getMgrId());
                if ("".equals(year) || year == null) {
                    map.put("month", AdcFunction.getDate(j));
                } else {
                    String month = j > 9 ? String.valueOf(j) : "0" + String.valueOf(j);
                    map.put("month", year + month);
                }
                try {
                    countMonth = smsCountMonthMapper.accountCountDate(map);
                    if (countMonth == null) {
                        countMonth = tempCountMonth;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("获取项目报账数据");
                    throw new SmsException("获取项目报账数据异常");
                }
                dataList.add(countMonth);
            }
            list.get(i).setList(dataList);
        }
        return list;
    }

    @SystemServiceLog(description = "报账统计和年度统计", value = false)
    public PageInfo<ReportMonth> findSmsCount
            (int page, int limit, String year) {
        PageInfo<ReportMonth> pageInfo;
        try {
            List dataList=changeData(this.getDataList(year));//数据源
            List tempList=new ArrayList();//临时的分页数据
            for (int i=0;i<limit;i++){
                int seq=(page-1)*10+i;
                if (seq<dataList.size())
                tempList.add(dataList.get(seq));
            }
            pageInfo = new PageInfo<ReportMonth>(tempList);
            pageInfo.setTotal((long)dataList.size());
            if ("".equals(year) || year == null) {
                commonService.createSmsOperLog("4", "报账统计", "0", "报账统计");
            } else {
                commonService.createSmsOperLog("4", "年度统计", "0", "年度统计");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报账统计和年度统计失败");
            if ("".equals(year) || year == null) {
                commonService.createSmsOperLog("4", "报账统计", "1", "报账统计");
                throw new SmsException("报账统计失败");
            } else {
                commonService.createSmsOperLog("4", "年度统计", "1", "年度统计");
                throw new SmsException("年度统计失败");
            }
        }
        return pageInfo;
    }

    @SystemServiceLog(description = "报账导出", value = false)
    public JSONArray download
            (String year) {
        {
            JSONArray jsonArray;
            try {
                jsonArray = JSONArray.parseArray(JSONArray.toJSONString(changeData(this.getDataList(year))));
                if ("".equals(year) || year == null) {
                    commonService.createSmsOperLog("4", "报账统计导出", "0", "报账统计");
                } else {
                    commonService.createSmsOperLog("4", "年度统计导出", "0", "年度统计");
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("报账统计和年度统计失败");
                if ("".equals(year) || year == null) {
                    commonService.createSmsOperLog("4", "报账统计导出", "1", "报账统计");
                    throw new SmsException("报账统计失败");
                } else {
                    commonService.createSmsOperLog("4", "年度统计导出", "1", "年度统计");
                    throw new SmsException("年度统计失败");
                }
            }
            return jsonArray;
        }
    }

    /**
     * 数据解析
     *
     * @param dataList
     * @return
     */
    public List<ReportMonth> changeData(List<SmsCountMonth> dataList) {
        List<ReportMonth> list = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            ReportMonth reportMonth = new ReportMonth();
            SmsCountMonth smsCountMonth = dataList.get(i);
            reportMonth.setMgrId(smsCountMonth.getMgrId());
            reportMonth.setMgrName(smsCountMonth.getMgrName());
            reportMonth.setCityId(smsCountMonth.getCityId());
            reportMonth.setCityName(smsCountMonth.getCityName());
            reportMonth.setLong1(smsCountMonth.getList().get(0).getLongNum());
            reportMonth.setLong2(smsCountMonth.getList().get(1).getLongNum());
            reportMonth.setLong3(smsCountMonth.getList().get(2).getLongNum());
            reportMonth.setLong4(smsCountMonth.getList().get(3).getLongNum());
            reportMonth.setLong5(smsCountMonth.getList().get(4).getLongNum());
            reportMonth.setLong6(smsCountMonth.getList().get(5).getLongNum());
            reportMonth.setLong7(smsCountMonth.getList().get(6).getLongNum());
            reportMonth.setLong8(smsCountMonth.getList().get(7).getLongNum());
            reportMonth.setLong9(smsCountMonth.getList().get(8).getLongNum());
            reportMonth.setLong10(smsCountMonth.getList().get(9).getLongNum());
            reportMonth.setLong11(smsCountMonth.getList().get(10).getLongNum());
            reportMonth.setLong12(smsCountMonth.getList().get(11).getLongNum());
            reportMonth.setNormal1(smsCountMonth.getList().get(0).getNormalNum());
            reportMonth.setNormal2(smsCountMonth.getList().get(1).getNormalNum());
            reportMonth.setNormal3(smsCountMonth.getList().get(2).getNormalNum());
            reportMonth.setNormal4(smsCountMonth.getList().get(3).getNormalNum());
            reportMonth.setNormal5(smsCountMonth.getList().get(4).getNormalNum());
            reportMonth.setNormal6(smsCountMonth.getList().get(5).getNormalNum());
            reportMonth.setNormal7(smsCountMonth.getList().get(6).getNormalNum());
            reportMonth.setNormal8(smsCountMonth.getList().get(7).getNormalNum());
            reportMonth.setNormal9(smsCountMonth.getList().get(8).getNormalNum());
            reportMonth.setNormal10(smsCountMonth.getList().get(9).getNormalNum());
            reportMonth.setNormal11(smsCountMonth.getList().get(10).getNormalNum());
            reportMonth.setNormal12(smsCountMonth.getList().get(11).getNormalNum());
            reportMonth.setLongSplit1(smsCountMonth.getList().get(0).getLongNumSplit());
            reportMonth.setLongSplit2(smsCountMonth.getList().get(1).getLongNumSplit());
            reportMonth.setLongSplit3(smsCountMonth.getList().get(2).getLongNumSplit());
            reportMonth.setLongSplit4(smsCountMonth.getList().get(3).getLongNumSplit());
            reportMonth.setLongSplit5(smsCountMonth.getList().get(4).getLongNumSplit());
            reportMonth.setLongSplit6(smsCountMonth.getList().get(5).getLongNumSplit());
            reportMonth.setLongSplit7(smsCountMonth.getList().get(6).getLongNumSplit());
            reportMonth.setLongSplit8(smsCountMonth.getList().get(7).getLongNumSplit());
            reportMonth.setLongSplit9(smsCountMonth.getList().get(8).getLongNumSplit());
            reportMonth.setLongSplit10(smsCountMonth.getList().get(9).getLongNumSplit());
            reportMonth.setLongSplit11(smsCountMonth.getList().get(10).getLongNumSplit());
            reportMonth.setLongSplit12(smsCountMonth.getList().get(11).getLongNumSplit());
            reportMonth.setLongCount1(smsCountMonth.getList().get(0).getLongNumCount());
            reportMonth.setLongCount2(smsCountMonth.getList().get(1).getLongNumCount());
            reportMonth.setLongCount3(smsCountMonth.getList().get(2).getLongNumCount());
            reportMonth.setLongCount4(smsCountMonth.getList().get(3).getLongNumCount());
            reportMonth.setLongCount5(smsCountMonth.getList().get(4).getLongNumCount());
            reportMonth.setLongCount6(smsCountMonth.getList().get(5).getLongNumCount());
            reportMonth.setLongCount7(smsCountMonth.getList().get(6).getLongNumCount());
            reportMonth.setLongCount8(smsCountMonth.getList().get(7).getLongNumCount());
            reportMonth.setLongCount9(smsCountMonth.getList().get(8).getLongNumCount());
            reportMonth.setLongCount10(smsCountMonth.getList().get(9).getLongNumCount());
            reportMonth.setLongCount11(smsCountMonth.getList().get(10).getLongNumCount());
            reportMonth.setLongCount12(smsCountMonth.getList().get(11).getLongNumCount());
            reportMonth.setNormalCount1(smsCountMonth.getList().get(0).getNormalNumCount());
            reportMonth.setNormalCount2(smsCountMonth.getList().get(1).getNormalNumCount());
            reportMonth.setNormalCount3(smsCountMonth.getList().get(2).getNormalNumCount());
            reportMonth.setNormalCount4(smsCountMonth.getList().get(3).getNormalNumCount());
            reportMonth.setNormalCount5(smsCountMonth.getList().get(4).getNormalNumCount());
            reportMonth.setNormalCount6(smsCountMonth.getList().get(5).getNormalNumCount());
            reportMonth.setNormalCount7(smsCountMonth.getList().get(6).getNormalNumCount());
            reportMonth.setNormalCount8(smsCountMonth.getList().get(7).getNormalNumCount());
            reportMonth.setNormalCount9(smsCountMonth.getList().get(8).getNormalNumCount());
            reportMonth.setNormalCount10(smsCountMonth.getList().get(9).getNormalNumCount());
            reportMonth.setNormalCount11(smsCountMonth.getList().get(10).getNormalNumCount());
            reportMonth.setNormalCount12(smsCountMonth.getList().get(11).getNormalNumCount());
            list.add(reportMonth);
        }
        return list;
    }

}

